//
//  OtherTableViewCell.m
//  project
//
//  Created by 张振宇 on 15/10/26.
//  Copyright © 2015年 lanou. All rights reserved.
//

#import "OtherTableViewCell.h"
#import <UIImageView+WebCache.h>
#import "FMDatabase.h"

@interface OtherTableViewCell()
@property (nonatomic, strong) UIImageView *imgView;
@property (nonatomic, strong) UILabel *titleLabel;


@property(nonatomic,strong)FMDatabase *dataBase;
@property(nonatomic,strong)NSMutableArray *array;

@end


@implementation OtherTableViewCell

- (void)awakeFromNib {
    // Initialization code
}

-(NSMutableArray *)array
{
    if (_array == nil)
    {
        _array = [NSMutableArray array];
    }
    return _array;
}

- (void)setConfigure:(OtherTableViewModel *)configure
{
    _configure = configure;
    [_imgView sd_setImageWithURL:[NSURL URLWithString:configure.cover_image_url]];
    _titleLabel.text = configure.title;
    [_btn setTitle:[NSString stringWithFormat:@"%@",configure.likes_count] forState:UIControlStateNormal];
    
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        _imgView = [[UIImageView alloc]initWithFrame:CGRectMake(5, 5, kWidth - 10, 190)];
        [self.contentView addSubview:_imgView];
        
        _titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 150, kWidth - 10, 40)];
        _titleLabel.backgroundColor = [UIColor clearColor];
        [_titleLabel setTextColor:[UIColor whiteColor]];
        [_imgView addSubview:_titleLabel];
        
        _btn = [UIButton buttonWithType:UIButtonTypeCustom];
        _btn.frame = CGRectMake(kWidth - 100, 10, 80, 40);
        _btn.layer.cornerRadius = 20;
        _btn.layer.masksToBounds = YES;
        [_btn setImage:[[UIImage imageNamed:@"iconfont-icon503"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
        [_btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_btn setBackgroundColor:[UIColor grayColor]];
        _btn.alpha = 0.5;
        [_btn addTarget:self action:@selector(like:) forControlEvents:(UIControlEventTouchUpInside)];
        [self.contentView addSubview:_btn];
    }
    return self;
}

-(void)like:(UIButton *)button
{
    [_array removeAllObjects];
    NSString *filepath = [[NSSearchPathForDirectoriesInDomains(9, 1, 1)firstObject] stringByAppendingPathComponent:@"project.db"];
    _dataBase = [FMDatabase databaseWithPath:filepath];
    if ([_dataBase open])
    {
        [_dataBase executeUpdate:@"create table AllLove (loveimage text,lovetitle text, loveid text)"];
    }
    else
    {
        NSLog(@"数据库打开失败");
    }
    
    FMResultSet *resultSet = [_dataBase executeQueryWithFormat:@"select * from AllLove"];
    while ([resultSet next])
    {
        NSString *modelId= [resultSet stringForColumn:@"loveid"];
        [self.array addObject:modelId];
        
    }
    for (NSString *str in self.array)
    {
        if ([[NSString stringWithFormat:@"%@",self.configure.Myid] isEqualToString:str])
        {
            [_dataBase executeUpdate:@"delete from AllLove where loveid=?", str];
            UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"取消收藏" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alertView show];
            [_btn setImage:[UIImage imageNamed:@"iconfont-icon503"] forState:(UIControlStateNormal)];
            return;
        }
    }
    
    [_dataBase executeUpdate:@"insert into AllLove values (? ,?, ?)",self.configure.cover_image_url,self.configure.title,[NSString stringWithFormat:@"%@",self.configure.Myid]];
    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"收藏成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alertView show];
    [_btn setImage:[UIImage imageNamed:@"iconfont-icon503-2"] forState:(UIControlStateNormal)];
    
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
